﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GlxRRSDB.ReportDataSetTableAdapters;
using GlxRRSDB;

namespace GlxRRSMain.UserControls
{
    public partial class FilmTargetManagementControl : UserControl
    {
        private tblCinemaTableAdapter x_tblCinemaTableAdapter = new tblCinemaTableAdapter();
        private List<ReportDataSet.tblCinemaRow> x_ListCinema = null;
        //
        private tblFilmTableAdapter x_tblFilmTableAdapter = new tblFilmTableAdapter();
        private List<ReportDataSet.tblFilmRow> x_ListFilm = null;
        ReportDataSet.tblFilmRow x_CurrentSelectedFilm = null;
        //
        private tblTargetTableAdapter x_tblTargetTableAdapter = new tblTargetTableAdapter();

        //
        public FilmTargetManagementControl()
        {
            InitializeComponent();
            initDataGridView();
        }
        private void initDataGridView()
        {
            initFilmDataGridView();
        }
        private void initFilmDataGridView()
        {
            gridFilm.AutoGenerateColumns = false;

            DataGridViewTextBoxColumn IDColumn = new DataGridViewTextBoxColumn();
            IDColumn.DataPropertyName = "ID";
            IDColumn.HeaderText = "Film ID";

            DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn();
            nameColumn.DataPropertyName = "FilmTitle";
            nameColumn.HeaderText = "Film Title";

            DataGridViewTextBoxColumn screen1Column = new DataGridViewTextBoxColumn();
            screen1Column.DataPropertyName = "StartDate";
            screen1Column.HeaderText = "Start Date";

            DataGridViewTextBoxColumn screen2Column = new DataGridViewTextBoxColumn();
            screen2Column.DataPropertyName = "FinishDate";
            screen2Column.HeaderText = "Finish Date";

            DataGridViewTextBoxColumn screen3Column = new DataGridViewTextBoxColumn();
            screen3Column.DataPropertyName = "Status";
            screen3Column.HeaderText = "Status";

            gridFilm.Columns.Add(IDColumn);
            gridFilm.Columns.Add(nameColumn);
            gridFilm.Columns.Add(screen1Column);
            gridFilm.Columns.Add(screen2Column);
            gridFilm.Columns.Add(screen3Column);
        }
        private static void BindField(Control control, string propertyName, object dataSource, string dataMember)
        {
            Binding bd;
            for (int index = control.DataBindings.Count - 1; (index == 0); index--)
            {
                bd = control.DataBindings[index];
                if (bd.PropertyName == propertyName)
                    control.DataBindings.Remove(bd);
            }
            control.DataBindings.Add(propertyName, dataSource, dataMember, true, DataSourceUpdateMode.Never);
        }
        private void bindingData()
        {
            if (CurrentSelectedFilm != null)
            {
                BindField(txtFilmID, "Text", CurrentSelectedFilm, "ID");
                BindField(txtFilmTitle, "Text", CurrentSelectedFilm, "FilmTitle");
                BindField(dpickerStartDate, "Value", CurrentSelectedFilm, "StartDate");
                BindField(dpickerFinishDate, "Value", CurrentSelectedFilm, "FinishDate");
            }
        }
        public ReportDataSet.tblFilmRow CurrentSelectedFilm
        {
            get
            {
                if (x_CurrentSelectedFilm == null)
                {
                    if (gridFilm.CurrentRow != null)
                    {
                        x_CurrentSelectedFilm = ListFilm[gridFilm.CurrentRow.Index];
                    }
                }
                return x_CurrentSelectedFilm;
            }
            set
            {
                x_CurrentSelectedFilm = value;
            }
        }
        public List<ReportDataSet.tblFilmRow> ListFilm
        {
            get
            {
                if (x_ListFilm == null)
                {
                    x_ListFilm = x_tblFilmTableAdapter.GetData().ToList();
                }
                return x_ListFilm;
            }
            set
            {
                x_ListFilm = value;
            }
        }
        private void gridFilm_SelectionChanged(object sender, EventArgs e)
        {
            x_CurrentSelectedFilm = null;
            bindingData();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            int filmID = int.Parse(txtFilmID.Text);
            string cinemaName = txtFilmTitle.Text;
            //
            ReportDataSet.tblFilmDataTable t = new ReportDataSet.tblFilmDataTable();
            x_tblFilmTableAdapter.Insert(cinemaName, dpickerStartDate.Value, dpickerFinishDate.Value, "1");
            
            ListFilm = null;
            gridFilm.DataSource = ListFilm;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int filmID = int.Parse(txtFilmID.Text);
            string cinemaName = txtFilmTitle.Text;
            //
            ReportDataSet.tblFilmRow a1 = x_tblFilmTableAdapter.GetData()
                .Where(kvp => kvp.ID == filmID)
                .First();
            a1.FilmTitle = cinemaName;
            a1.StartDate = dpickerStartDate.Value;
            a1.FinishDate = dpickerFinishDate.Value;
            x_tblFilmTableAdapter.Update(a1);
            ListFilm = null;
            gridFilm.DataSource = ListFilm;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtFilmTitle.Text = "";
        }

        public List<ReportDataSet.tblCinemaRow> ListCinema
        {
            get
            {
                if (x_ListCinema == null)
                {
                    x_ListCinema = x_tblCinemaTableAdapter.GetData().ToList();
                }
                return x_ListCinema;
            }
            set
            {
                x_ListCinema = value;
            }
        }
        private void FilmTargetManagementControl_Load(object sender, EventArgs e)
        {
            gridFilm.DataSource = ListFilm;
            bindingData();
        }
    }
}
